import 'package:bruno/bruno.dart';
import 'package:flutter/material.dart';

import 'package:intl/intl.dart';

import '../bean/AbstactCardContainerBean.dart';
import '../config/StyleConfig.dart';
import '../res/HDColors.dart';
import 'CardContainerHorizontal.dart';


///auther:lxf
///action:选择日期  可以根据参数控制类型
///date:20220701
///state:完成
class FieldDate extends FormField<String> implements CardContainerBean {
  @override
  bool? autoValidate;

  @override
  Widget? content;

  @override
  String? errorText;

  @override
  bool? isRequired;

  @override
  String? label;

  @override
  String? hintLabel;

  @override
  MainAxisAlignment? labelAlign;

  @override
  double? labelIconSize;

  @override
  TextStyle? labelStyle;

  @override
  Widget? labelSuffix;

  @override
  double? labelWitch;

  @override
  Icon? leftIcon;

  @override
  Icon? rightIcon;

  @override
  bool? showLine;

  @override
  bool? visible;

  @override
  bool? readOnly;

  @override
  FormFieldSetter<String>? onSaved;
  @override
  FormFieldValidator<String>? validator;

  final String initialValue;//表示初始值
  final ValueChanged<dynamic>? onChanged;//表示change事件
  final String? minDateTime;//表示最小时间
  final String? maxDateTime;//表示最大日期
  final BrnDateTimePickerMode pickerMode;///表示控件模式  日期、时间
  FieldDate({
    Key? key,
    this.errorText,
    required this.label,
    this.labelAlign,
    this.labelIconSize,
    this.labelStyle,
    this.labelSuffix,
    this.labelWitch,
    this.leftIcon,
    this.rightIcon,
    this.showLine = true,
    this.visible,
    this.initialValue = "",
    this.readOnly=false,
    this.onSaved,
    this.validator,
    this.onChanged,
    this.isRequired,
    //this.autoValidate,
    this.maxDateTime,
    this.minDateTime,
    this.pickerMode=BrnDateTimePickerMode.datetime,
  }) : super(
            key: key,
            initialValue: initialValue,
            validator:validator,
            onSaved: onSaved,
            builder: (field){
              return (field as _FieldDateState)._build(field.context);
            });

  @override
  _FieldDateState createState() => _FieldDateState();
}

class _FieldDateState extends FormFieldState<String> {
  //表示日期控件默认展示的样式
  String format = "yyyy年,MM月,dd日,HH时,mm分";
  DateTime? initialDateTime;//表示初始化日期

  @override
  FieldDate get widget => super.widget as FieldDate;

  @override
  void initState() {
    super.initState();
    // setState(() {
    // });
    initData();
  }
  initData(){
    setValue(widget.initialValue);
    if(this.value==""){
      //赋值当前时间
      initialDateTime=DateTime.now();// HDDateFormat.today();
    }else{
      initialDateTime= DateTime.parse(this.value as String);
    }
  }

  @override
  void didUpdateWidget(FieldDate oldWidget) {
    initData();
    super.didUpdateWidget(oldWidget);
  }

  Widget _build(BuildContext context) {
    return GestureDetector(
      onTap: () {
        //表示 非 只读 时才可以点击
        if(!(widget.readOnly??false)){
          _showDialog(widget.label ?? "");
        }
      },
      child: CardContainerHorizontal(
        label: widget.label,
        visible: widget.visible,
        leftIcon: widget.leftIcon,
        errorText: errorText,
        showLine: widget.showLine,
        isRequired: widget.isRequired,
        rightIcon: widget.readOnly??false?null:Icon(color:HDColors.formTitleColor,Icons.keyboard_arrow_right),
        content: resultItem(),
      ),
    );
  }

  ///表示显示字典结果
  Wrap resultItem() {
    List<Widget> listcontent = [];
    listcontent.add(Text(this.value!=""?(this.value as String):"请选择${widget.hintLabel??widget.label}",style: this.value!=""?TextStyle(
    color: HDColors.siGrayHint,
    fontSize: StyleConfig.fontContentSize16,
  ):TextStyle(
    color:HDColors.siGrayHint,
    fontSize: StyleConfig.fontContentSize16,
  )));
    return Wrap(
      spacing: 10,
      alignment: WrapAlignment.end,
      children: listcontent,
    );
  }

  ///弹框选择日期
  void _showDialog(String label) async {
    BrnDatePicker.showDatePicker(context,
        // maxDateTime: DateTime.parse(MAX_DATETIME),
        minDateTime: (widget.minDateTime != null && widget.minDateTime!="") ? DateTime.parse(widget.minDateTime!) : null,
        initialDateTime: initialDateTime,
        // 支持DateTimePickerMode.date、DateTimePickerMode.datetime、DateTimePickerMode.time
        pickerMode:widget.pickerMode,
        //minuteDivider: 30,//
        pickerTitleConfig: BrnPickerTitleConfig.Default,
        dateFormat: format,
        onConfirm: (dateTime, list) {
          initialDateTime=dateTime;
          ///表示值的变化------
          _handleOnChanged(DateFormat("yyyy-MM-dd HH:mm:ss").format(dateTime));
        });
  }
  ///表示走 统一的change方法----
  void _handleOnChanged(String _value) {
    if (this.value != _value) {
      didChange(_value);
      if (widget.onChanged != null) {
        widget.onChanged!(_value);
      }
    }
  }


}
